package com.city.springcloud.controller;

import com.city.springcloud.entity.Dept;
import com.city.springcloud.service.DeptDaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Cheng
 * Date: 2020/4/25
 * Time: 23:19
 * Description: No Description
 *
 * 提供者服务
 * 通过maven导入实体类操作，提供服务给消费者
 */
@RestController
public class DetpContrellor {
    @Autowired
    private DeptDaoService deptDaoService;

    /**
     * 获取服务注册信息，利于团队开发
     */
    @Autowired
    private DiscoveryClient discoveryClient;

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept){
        return deptDaoService.addDept(dept);
    }

    @GetMapping("/dept/list")
    public List<Dept> list(){
        return deptDaoService.selectDept();
    }

    /**携带服务注册的信息到服务注册中心*/
    @GetMapping("/dept/discovery")
    public Object discovery(){
        //获取微服务列表清单
        List<String> services = discoveryClient.getServices();
        List<ServiceInstance> instances = discoveryClient.getInstances("city");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost()+instance.getPort()+instance.getUri()+instance.getServiceId()+"/n");
        }
        return this.discoveryClient;
    }
}